﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace database_entry{
/// <summary>
/// Summary description for DBEntry
/// </summary>
public class DBEntry
{
	private int scheduleID;
    private int clientID;
    public string note;
    private SqlDateTime startTime;
    private SqlDateTime endTime;
    private SqlDateTime startDate;
    private SqlDateTime endDate;
    private bool daily,weekly,monthly;
    private string daysOfMonth;
    private bool mon,tue,wed,thu,fri,sat,sun;

    #region Constructors

    public DBEntry()
    {
        setNull();
    }
    public DBEntry(int sID, int cID)
    {
        setNull();
        scheduleID = sID;
        clientID = cID;
    }
    public DBEntry(int sID, int cID, string msg)
    {
        setNull();
        scheduleID = sID;
        clientID = cID;
        note = msg;
    }
    public DBEntry(int sID, int cID, string msg, SqlDateTime sT, SqlDateTime eT, SqlDateTime sD)
    {
        setNull();
        scheduleID = sID;
        clientID = cID;
        note = msg;
        startTime = sT;
        endTime = eT;
        startDate = sD;
    }
    public DBEntry(int sID, int cID, string msg, SqlDateTime sT, SqlDateTime eT, SqlDateTime sD, SqlDateTime eD)
    {
        setNull();
        scheduleID = sID;
        clientID = cID;
        note = msg;
        startTime = sT;
        endTime = eT;
        startDate = sD;
        endDate = eD;
    }
    #endregion

    #region Recurrence

    public void setRecurrence(bool isDaily, bool isWeekly, bool isMonthly)
    {
        daily = isDaily;
        weekly = isWeekly;
        monthly = isMonthly;
    }
    public void setWeeklyRecurrence(bool Monday, bool Tuesday, bool Wednesday, bool Thursday, bool Friday, bool Saturday, bool Sunday)
    {
        mon = Monday;
        tue = Tuesday;
        wed = Wednesday;
        thu = Thursday;
        fri = Friday;
        sat = Saturday;
        sun = Sunday;
    }

    //
    //allow input of weekly recurrence as a string of 0/1
    //e.g. Monday and Wednesday = "1010000"
    //
    public void setWeeklyRecurrence(string dayString)
    {
        mon = (dayString[0] == '1');
        tue = (dayString[1] == '1');
        wed = (dayString[2] == '1');
        thu = (dayString[3] == '1');
        fri = (dayString[4] == '1');
        sat = (dayString[5] == '1');
        sun = (dayString[6] == '1');
    }
    public void setMonthlyRecurrence(int[] days)
    {
        string s = "";
        for(int i=0; i<days.Length; i++)
        {
            if(days[i] < 31 && days[i] > 9)
            {
                s = s + days[i].ToString();
            }
            else if(days[i] > 0 && days[i] < 10)
            {
                s = s + "0"+days[i].ToString();
            }
        }
        daysOfMonth = s;
    }
    public void setMonthlyRecurrence(string days)
    {
        daysOfMonth = days;
    }

#endregion

    #region SQL Statement Creation
    public SqlCommand InsertItem()
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO data ([ScheduleID],[ClientID],[StartTime],[EndTime],[StartDate],[EndDate],[Note],"+
            "[Daily],[Weekly],[Monthly],[Mon],[Tue],[Wed],[Thu],[Fri],[Sat],[Sun],[DaysOfMonth]) " +
            "VALUES (@scheduleID,@clientID,@startTime,@endTime,@startDate,@endDate,@note,@daily,@weekly,@monthly," 
            + "@mon,@tue,@wed,@thu,@fri,@sat,@sun,@daysOfMonth)");
        SqlParameter p1 = new SqlParameter("@scheduleID",scheduleID);     cmd.Parameters.Add(p1);
        SqlParameter p2 = new SqlParameter("@clientID",clientID);         cmd.Parameters.Add(p2);
        SqlParameter p3 = new SqlParameter("@startTime",startTime);       cmd.Parameters.Add(p3);
        SqlParameter p4 = new SqlParameter("@endTime",endTime);           cmd.Parameters.Add(p4);
        SqlParameter p5 = new SqlParameter("@startDate",startDate);       cmd.Parameters.Add(p5);
        SqlParameter p6 = new SqlParameter("@endDate",endDate);           cmd.Parameters.Add(p6);
        SqlParameter p7 = new SqlParameter("@note",note);                 cmd.Parameters.Add(p7);
        SqlParameter p8 = new SqlParameter("@daily",daily);               cmd.Parameters.Add(p8);
        SqlParameter p9 = new SqlParameter("@weekly",weekly);             cmd.Parameters.Add(p9);
        SqlParameter p10 = new SqlParameter("@monthly",monthly);          cmd.Parameters.Add(p10);
        SqlParameter p11 = new SqlParameter("@mon",mon);                  cmd.Parameters.Add(p11);
        SqlParameter p12 = new SqlParameter("@tue",tue);                  cmd.Parameters.Add(p12);
        SqlParameter p13 = new SqlParameter("@wed",wed);                  cmd.Parameters.Add(p13);
        SqlParameter p14 = new SqlParameter("@thu",thu);                  cmd.Parameters.Add(p14);
        SqlParameter p15 = new SqlParameter("@fri",fri);                  cmd.Parameters.Add(p15);
        SqlParameter p16 = new SqlParameter("@sat",sat);                  cmd.Parameters.Add(p16);
        SqlParameter p17 = new SqlParameter("@sun",sun);                  cmd.Parameters.Add(p17);
        SqlParameter p18 = new SqlParameter("@daysOfMonth",daysOfMonth);  cmd.Parameters.Add(p18);
        return cmd;
    }
    #endregion


    private void setNull()
    {
        scheduleID = 0;
        clientID = 0;
        note = "";
        startTime = new DateTime();
        endTime = new DateTime();
        startDate = new DateTime();
        endDate = new DateTime();
        daily = weekly = monthly = false;
        mon = tue = wed = thu = fri = sat = sun = false;

    }
	}
}
